
from itsdangerous import TimedJSONWebSignatureSerializer as Serializer, BadSignature
from mall import settings
def generic_openid_token(openid):

    serializer = Serializer(settings.SECRET_KEY, expires_in=3600)
    data = {'openid': openid}
    token = serializer.dumps(data)
    return token.decode()

def check_access_token(token):

    #1. 创建序列化器
    serializer = Serializer(secret_key=settings.SECRET_KEY, expires_in=3600)
    #2. 对数据进行解密
    try:
        data = serializer.loads(token)
        # data = {'openid':openid}
    # except BadTimeSignature:
    except BadSignature:
        return None


    openid = data.get('openid')

    return openid



def generic_access_token(access_token):
    serializer = Serializer(settings.SECRET_KEY, expires_in=3600)
    data = {'access_token': access_token}
    token = serializer.dumps(data)
    print(token)
    return token.decode()


def check_weibo_access_token(access_token):
    #1. 创建序列化器
    serializer = Serializer(secret_key=settings.SECRET_KEY, expires_in=3600)
    #2. 对数据进行解密
    try:
        data = serializer.loads(access_token)
        # data = {'openid':openid}
    # except BadTimeSignature:
    except BadSignature:
        return None


    access_token = data.get('access_token')

    return access_token